import $https from '@/api/https'
export default {
  namespaced: true,
  state: {
    currentStudentInfo: {}, // 学员信息
    currentCoachInfo: {},   // 当前学员的教练信息
    AllCoachName: [],       // 所有教练名
  },
  mutations: {
    setCurrentStudentInfo(state, val) { // 设置当前学员信息
      state.currentStudentInfo = val
    },
    setCurrentCoachInfo(state, val) { // 设置当前学员的教练信息
      state.currentCoachInfo = val
    },
    setAllCoachName(state, val) { // 设置所有教练姓名
      state.AllCoachName = val
    },
  },
  actions: {
    // 根据 [identify] 属性获取【学员信息】
    async getCurrentStudentInfo(context) {
      const { data } = await $https.getUserData({
        url: "/student",
        identify: JSON.parse(localStorage.getItem("token")).username
      });
      context.commit("setCurrentStudentInfo", data[0])
    },
    // 获取所有教练姓名
    async getAllCoachName(context) {
      let names = []
      const { data } = await $https.getUserData({ url: "/coach" });
      data.forEach((element) => {
        names.push(element.name);
      });
      context.commit("setAllCoachName", names)
    },
    // 根据当前学员的 [coachID] 属性获取【教练信息】
    async getCoachIfoByCurrentStudentCoachID(context, coachID) {
      const { data } = await $https.getUserData({
        url: "/coach",
        coachID: coachID,
      });
      context.commit("setCurrentCoachInfo", data[0])
    },
  }
}